(function() {
	require(['../js/tools/getarea']);
	Path.on("/user/__basename__.html", function(_current_location) {
		var queryString = _current_location.query;
		var _type = queryString.get("type");
		if (_type == 2) {
			App.set('$Cache.user.info.title', '密码管理');
			App.set('$Cache.user.info.infotype', 'pwd');
		} else if (_type == 3) {
			App.set('$Cache.user.info.title', '收货地址');
			App.set('$Cache.user.info.infotype', 'addr');
		} else {
			App.set('$Cache.user.info.title', '基本信息');
			App.set('$Cache.user.info.infotype', '');
		};

		// 地址
		coAjax.get(appConfig.user.address_detail, function(result) {
			App.set("$Cache.user.info.address_detail", result.result);
		});
		// 增加
		App.set("$Event.user.info.add_user_address", function() {
			var new_user_address = {
				province: $("[name='province']").find('option:selected').text(),
				town: $("[name='city']").val(),
				county: $("[name='area']").val(),
				// street:
				detail: $("[name='detailed_addre']").val(),
				mobilenumber: $("[name='mobile_num']").val(),
				phonenumber: $("[name='phone_num']").val(),
				postcode: $("[name='zip_code']").val(),
				name: $("[name='name']").val()
			};
			console.log(new_user_address);
			coAjax.post(appConfig.user.add_address, new_user_address, function(result) {
				var address_detail = App.get("$Cache.user.info.address_detail");
				address_detail.unshift(result.result);
				App.set("$Cache.user.info.address_detail", address_detail);
				App.model.toggle('isnew');
				alert("success", "添加新地址成功！");
				coAjax.put(appConfig.user.set_default_address, {
					address_id: result.result._id
				}, function(result) {
					App.set("loginer.default_ua_id", result.result);
					App.getModel().touchOff("$Cache.user.info.address_detail");
					alert('success', '已将新增加地址修改为默认地址！');
				});
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
			});
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
		});

	});
	// 个人资料
	App.set("$Event.user.info.post_edit_user_info", function(e) {
		coAjax.put(appConfig.user.set_info_url, App.get("loginer.info"), function(result) {
			// console.log(result);
			alert('success', '修改成功');
			App.set("loginer", result.result);
		}, function(errorCode) {
			switch (errorCode) {
				case "00025":
					alert("error", "登录超时");
					href.toLogin();
					break;
				case "00041":
					alert("error", "请检查您的身份证格式");
					break;
				case "00043":
					alert("error", "请检查您的邮箱格式");
					break;
				case "00044":
					alert("error", "请检查您的手机号码");
					break;
				default:
					alert(arguments[2]);
			};
		});
	});
	// 密码
	App.set("$Event.user.info.changepwd.change", function(e, vm) {
		var current_pwd = App.get("$Cache.user.info.changepwd.current_pwd");
		var new_pwd = App.get("$Cache.user.info.changepwd.new_pwd");
		var new_pwd_bak = App.get("$Cache.user.info.changepwd.new_pwd_bak");
		if (!current_pwd) {
			alert("warn", "请输入当前密码");
			return;
		}
		if (new_pwd !== new_pwd_bak) {
			alert("warn", "两次密码不匹配");
			return;
		}
		coAjax.put(appConfig.user.change_password, {
			old_password: current_pwd,
			new_password: new_pwd
		}, function(result) {
			App.set("$Cache.user.info.changepwd.current_pwd", "");
			App.set("$Cache.user.info.changepwd.new_pwd", "");
			App.set("$Cache.user.info.changepwd.new_pwd_bak", "");
			alert("success", "密码修改成功");
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
		});
	});
	// 修改默认
	App.set("$Event.user.info.set_default_address", function(e, vm) {
		coAjax.put(appConfig.user.set_default_address, {
			address_id: vm.get("_id")
		}, function(result) {
			App.set("loginer.default_ua_id", result.result);
			App.getModel().touchOff("$Cache.user.info.address_detail");
			alert('success', '更改默认地址成功');
		});
	}, function(errorCode, xhr, errorMsg) {
		alert("error", errorMsg);
	});
	App.set("$Event.user.info.remove_address", function(e, vm) {
		coAjax["delete"](appConfig.user.remove_address, {
			address_id: vm.get("_id")
		}, function(result) {
			alert('success', '删除成功');
			App.set("$Cache.user.info.address_detail", result.result);
		})
	}, function(errorCode, xhr, errorMsg) {
		alert("error", errorMsg);
	});
	App.set('$Event.user.info.addaddr', function() {
		App.model.toggle('$Cache.user.info.isnew');
	});
	App.set('$Event.user.info.removeaddr', function() {
		App.model.toggle('$Cache.user.info.isremove');
	});
}());

function getPath(obj) {
	if (obj) {
		var files = obj.files;
		var type = files[0].type;
		if (type === "image/jpeg" || type === "image/bmp") {
			$('img[name="img"]')[0].src = window.URL.createObjectURL(files[0]);
			try {
				window.URL.revokeObjectURL(this.src);
			} catch (e) {}
		} else {
			alert('只支持.jpg, .jpeg, .bmp格式图片');
		}
	}
}